package com.hirsi.module.manager.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hirsi.core.expand.TableRUpdate;
import com.hirsi.module.manager.convert.ManagerConvert;
import com.hirsi.module.manager.entity.Manager;
import com.hirsi.module.manager.entity.RManagerDepartment;
import com.hirsi.module.manager.entity.RManagerRole;
import com.hirsi.module.manager.mapper.ManagerMapper;
import com.hirsi.module.manager.service.ManagerService;
import com.hirsi.module.manager.service.RManagerDepartmentService;
import com.hirsi.module.manager.service.RManagerRoleService;
import com.hirsi.module.manager.vo.EditManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author Hirsi
 * @since 2024-03-11
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    private RManagerRoleService managerRoleService;

    @Autowired
    private RManagerDepartmentService managerDepartmentService;

    @Override
    public EditManagerVo edit(Integer id) {
        Manager manager = lambdaQuery()
                .eq(Manager::getId, id)
                .select(Manager::getUsername, Manager::getName, Manager::getNick,
                        Manager::getPhone, Manager::getEmail, Manager::getGender,
                        Manager::getAvatar, Manager::getStatus).one();
        if (manager == null) throw new RuntimeException("管理员不存在");
        EditManagerVo vo = ManagerConvert.INSTANCE.ManagerToEditManagerVo(manager);
        List<RManagerRole> managerRoles = managerRoleService.lambdaQuery()
                .eq(RManagerRole::getManagerId, id)
                .select(RManagerRole::getRoleId).list();
        if (CollUtil.isNotEmpty(managerRoles)) vo.setRoleIds(managerRoles.stream().map(RManagerRole::getRoleId).collect(Collectors.toList()));
        List<RManagerDepartment> managerDepartments = managerDepartmentService.lambdaQuery()
                .eq(RManagerDepartment::getManagerId, id)
                .select(RManagerDepartment::getDepartmentId).list();
        if (CollUtil.isNotEmpty(managerDepartments)) vo.setDepartmentIds(managerDepartments.stream().map(RManagerDepartment::getDepartmentId).collect(Collectors.toList()));
        return vo;
    }

    @Override
    public void save(EditManagerVo vo) {
        //新增或更新管理员
        Manager manager = ManagerConvert.INSTANCE.EditManagerVoToManager(vo);
        saveOrUpdate(manager);

        //处理管理员角色
        List<RManagerRole> managerRoles = new ArrayList<>();
        if (CollUtil.isNotEmpty(vo.getRoleIds())) {
            managerRoles = vo.getRoleIds().stream().map(item -> new RManagerRole().setManagerId(manager.getId()).setRoleId(item)).collect(Collectors.toList());
        }
        List<RManagerRole> oldManagerRoles = managerRoleService.lambdaQuery()
                .eq(RManagerRole::getManagerId, manager.getId())
                .select(RManagerRole::getId, RManagerRole::getManagerId, RManagerRole::getRoleId).list();
        TableRUpdate.Result<RManagerRole> execute = TableRUpdate.execute(managerRoles, oldManagerRoles);
        if (CollUtil.isNotEmpty(execute.getSaves())) managerRoleService.saveOrUpdateBatch(execute.getSaves());
        if (CollUtil.isNotEmpty(execute.getDeleteIds())) managerRoleService.removeByIds(execute.getDeleteIds());

        //处理管理员部门
        List<RManagerDepartment> managerDepartments = new ArrayList<>();
        if (CollUtil.isNotEmpty(vo.getDepartmentIds())) {
            managerDepartments = vo.getDepartmentIds().stream().map(item -> new RManagerDepartment().setManagerId(manager.getId()).setDepartmentId(item)).collect(Collectors.toList());
        }
        List<RManagerDepartment> oldManagerDepartments = managerDepartmentService.lambdaQuery()
                .eq(RManagerDepartment::getManagerId, manager.getId())
                .select(RManagerDepartment::getId, RManagerDepartment::getManagerId, RManagerDepartment::getDepartmentId).list();
        TableRUpdate.Result<RManagerDepartment> departmentExecute = TableRUpdate.execute(managerDepartments, oldManagerDepartments);
        if (CollUtil.isNotEmpty(departmentExecute.getSaves())) managerDepartmentService.saveOrUpdateBatch(departmentExecute.getSaves());
        if (CollUtil.isNotEmpty(departmentExecute.getDeleteIds())) managerDepartmentService.removeByIds(departmentExecute.getDeleteIds());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        //删除管理员
        removeByIds(ids);
        //删除管理员角色中间表
        managerRoleService.lambdaUpdate()
                .in(RManagerRole::getManagerId, ids).remove();
        //删除管理员部门中间表
        managerDepartmentService.lambdaUpdate()
                .in(RManagerDepartment::getManagerId, ids).remove();
    }
}
